<?php
require_once("bean/user.class.php");
require_once("bean/page.class.php");
require_once("bean/category.class.php");
require_once("includes/util.php");

class userman 
{
	var $db;
 	var $smarty;
 	var $msg;
 	
    function userman($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
		
		$this->msg[1] = _("ERROR: Please type the Username.") . "<br />";
		$this->msg[2] = _("ERROR: Password fields can not be empty.") . "<br />";
		$this->msg[3] = _("ERROR: Please type the same password in the two password fields.") . "<br />";
		$this->msg[4] = _("ERROR: Username already registered.") . "<br />";
		$this->msg[5] = _("ERROR: Please type an e-mail address.") . "<br />";
		$this->msg[6] = _("ERROR: User ID doesn't exist.") . "<br />";
		$this->msg[7] = _("ERROR: Could not save to database.") . "<br />";
	}
	
	/**
	 * Process ajax call to add a user
	 */
	function addUser()
	{
		$arr = array();

		$arr['code'] = 0;
		$arr['message'] = "";
		$arr['sEdit'] 	= _("Edit");
		$arr['sDelete'] = _("Delete");
		
		if($_POST['user_login'] == "")
		{
			$arr['code'] += 1;
			$arr['message'] .= $this->msg[1];
		}
		
		if($_POST['user_pass'] == "")
		{
			$arr['code'] += 2;
			$arr['message'] .= $this->msg[2];
		}
		
		if($_POST['user_pass'] != $_POST['user_pass2'])
		{
			$arr['code'] += 3;
			$arr['message'] .= $this->msg[3];
		}
		
		if(user::existUser($this->db, $_POST['user_login']))
		{
			$arr['code'] += 4;
			$arr['message'] .= $this->msg[4];
		}
		
		if($_POST['user_email'] == "")
		{
			$arr['code'] += 5;
			$arr['message'] .= $this->msg[5];
		}
		
		if($arr['code'] == 0)
		{
			$arr['user_login'] = $_POST['user_login'];
			$arr['user_nicename'] = $_POST['user_nicename'];
			$arr['user_email'] = $_POST['user_email'];
			$arr['user_url'] = $_POST['user_url'];
			$arr['ID'] = user::saveUser($this->db, $_POST);
			
			if( $arr['ID'] > -1)
			{
				$arr['code'] = 0;
				$arr['message'] = _("New User has been saved.");
			}
			else
			{
				$arr['code'] = 4;
				$arr['message'] = _("ERROR: Could not save user to database.");
			}
		}
		
		echo array2json($arr);
	}
	
	/**
	 * Process ajax call to update user
	 */
	function editUser()
	{
		$arr = array();

		$arr['code'] = 0;
		$arr['message'] = "";
		
		if(user::existUserById($this->db, $_POST['user_id']) == false)
		{
			$arr['code'] += 6;
			$arr['message'] .= $this->msg[6];
		}
		else if($_POST['user_pass'] == "")
		{
			$arr['code'] += 2;
			$arr['message'] .= $this->msg[2];
		}
		else if($_POST['user_pass'] != $_POST['user_pass2'])
		{
			$arr['code'] += 3;
			$arr['message'] .= $this->msg[3];
		}
		else if($_POST['user_email'] == "")
		{
			$arr['code'] += 5;
			$arr['message'] .= $this->msg[5];
		}
		
		if($arr['code'] == 0)
		{
			$arr['message'] = _("User information has been updated.");
			
			$res = user::updateUser($this->db, $_POST);
			
			if($res == false)
			{
				$arr['code'] += 7;
				$arr['message'] = $this->msg[7];
			}
		}
		
		if($arr['code'] != 0)
		{
			$arr['message'] = "<b>". _("User information doesn't updated") . "</b>" . 
								"<br />" . $arr['message'];	
		}
		
		$this->smarty->assign('resp_message', $arr['message']);
		$this->showUserList();
	}
	
	/**
	 * Process ajax call to delete user
	 */
	function delUser()
	{
		$arr = array('code' => 0, 'message' => "");
		
		$id = @$_POST['id'];
		$res = user::deleteUserById($this->db, $id);
		if($res == true)
		{
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("User <u>%s</u> has been deleted."), $id);
		}
		else
		{
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delte user <u>%s</u>."), $id);
		}
		
		echo array2json($arr);
	}
	
	/**
	 * Process all request related to User functions.
	 * Add, Update, Delete
	 */
	function processUserList($sub1)
	{
		if(isset($_POST['act']))
		{
			$act = $_POST['act'];
			if($act == "adduser")
			{
				$this->addUser();
			}
			else if($act == "edituser")
			{
				$this->editUser();
			}
			else if($act == "deluser")
			{
				$this->delUser();
			}
		}
		else
		{
			if($sub1 == "edituser")
			{
				$id = @$_GET['id'];
				$user = user::getUserById($this->db, $id);
				
				if($user == null)
				{
					$this->showUserList();
					return;
				}
				
				$this->smarty->assign("THEUSER", $user);
				
				$this->smarty->display('userman.tpl');
			}
			else
			{
				$this->showUserList();
			}
		}
	}
	
	/**
	 * Show user table
	 */
	function showUserList()
	{
		$users = user::getAllUser($this->db);
		$this->smarty->assign("USERS", $users);
			
		$this->smarty->display('userman.tpl');
	}
	
	/**
	 * Process all request related to Profile function
	 */
	function processProfile($sub1)
	{
		$id = @$_SESSION['u_id'];
		$user = user::getUserById($this->db, $id);
		$this->smarty->assign("THEUSER", $user);
		
		$this->smarty->display('userman.tpl');
	}
	
	/**
	 * Process all request related to User Post function
	 */
	function processUserPost($sub1)
	{
		$id = @$_SESSION['u_id'];
		
		if(!isset($_POST['act']))
		{
			$page = page::getAllPageByCondition($this->db, "page_author=" . $id);
			
			$this->smarty->assign("PAGES", $page);
		}
		else
		{
			$act = $_POST["act"];
			
			if($act == "browse_cat")
			{
				$this->smarty->assign("COMMENT", 
					_("Results for browse page by category"));
					
				$pages = page::getAllPageByCat($this->db, $_POST['cat'], $id);
			
			}
			else if($act == "browse_month")
			{
				$this->smarty->assign("COMMENT", 
					_("Results for browse page by month"));
					
				$this->smarty->assign("SELMONTH", $_POST['Date_Month']);
					
				$pages = page::getAllPageByCondition($this->db, 
						"page_author=" . $id . 
						" AND MONTH(page_date) = " . $_POST['Date_Month']);
			}
			
			$this->smarty->assign("PAGES", $pages);
		}
		
		//Get all category for search
		$catList = category::getAllCategory($this->db, null);
		$this->smarty->assign("CATS", $catList);
		
		$this->smarty->display('userman.tpl');
	}
	
	/**
	 * This function will be call be index.php base on mod specified by GET param.
	 */
	function process()
	{
		$sub1 = "users";
		if(isset($_GET['sub1']))
		{
			$sub1 = $_GET['sub1'];
		}
		if($sub1 == "") $sub1 = "users";
		
		if($sub1 == "users" || $sub1 == "edituser")
		{
			$this->processUserList($sub1);
		}
		else if($sub1 == "profile")
		{
			$this->processProfile($sub1);
		}
		else if($sub1 == "userpost")
		{
			$this->processUserPost($sub1);
		}
	}
}
?>